<template>
    <div>
        <el-divider content-position='left'>
            <el-button type='primary' @click='createHandle'>添加字典</el-button>
            <el-button type='danger' v-if="history.length > 1" @click='backToParent'>返回上一级列表</el-button>
            <el-popover
                ref='popover'
                placement="bottom-start"
                width="500"
                trigger="hover"
                @hide='resetPopover'>
                <el-form label-width="120px">
                    <el-form-item label='字典名称'>
                        <el-input v-model='createForm.title'></el-input>
                    </el-form-item>
                    <el-form-item label='字典code/value'>
                        <el-input v-model='createForm.value'></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type='primary' @click='createConfirm'>确认</el-button>
                    </el-form-item>
                </el-form>
                <div slot='reference'></div>
                <!-- <el-button slot="reference"></el-button> -->
            </el-popover>
        </el-divider>
        <el-table
            :data="tableData"
            stripe
            border
            :header-cell-style="TABLE_HEADER_CELL_STYLE"
            v-loading = "isLoading"
            element-loading-background = "rgba(255, 255, 255, .5)"
            element-loading-text = "加载中，请稍后..."
            element-loading-spinner = "el-icon-loading"
            @expand-change="expandChange">
            <el-table-column type='expand'>
                <template v-slot='scope'>
                    <div style='padding: 15px 30px;'>
                        <!-- 字典详情 -->
                        <el-table
                            :data="scope.row.children"
                            stripe
                            border
                            :header-cell-style="TABLE_HEADER_CELL_STYLE"
                            v-loading = "isLoading"
                            element-loading-background = "rgba(255, 255, 255, .5)"
                            element-loading-text = "加载中，请稍后..."
                            element-loading-spinner = "el-icon-loading"
                            @expand-change="expandChange">
                            <el-table-column prop='id' label='id'></el-table-column>
                            <el-table-column prop='title' label='字典名称'>
                                <template v-slot='scope'>
                                    <edit-table-column v-model='scope.row' edit-key='title' @change='updateHandle(scope.row)'></edit-table-column>
                                </template>
                            </el-table-column>
                            <el-table-column prop='title' label='字典值'>
                                <template v-slot='scope'>
                                    <edit-table-column v-model='scope.row' edit-key='value' @change='updateHandle(scope.row)'></edit-table-column>
                                </template>
                            </el-table-column>
                            <el-table-column label='操作'>
                                <template v-slot='scope'>
                                    <el-popconfirm title="确认删除吗？" @confirm='deleteHandle(scope.row, true)' class="popconfirm">
                                        <el-button type='text' slot="reference">删除</el-button>
                                    </el-popconfirm>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop='title' label='字典名称'>
                <template v-slot='scope'>
                    <edit-table-column v-model='scope.row' edit-key='title' @change='updateHandle(scope.row)'></edit-table-column>
                </template>
            </el-table-column>
            <el-table-column prop='code' label='code'>
                <template v-slot='scope'>
                    <edit-table-column v-model='scope.row' edit-key='value' @change='updateHandle(scope.row)'></edit-table-column>
                </template>
            </el-table-column>
            <el-table-column label='操作'>
                <template v-slot='scope'>
                    <el-popconfirm title="确认删除吗？" @confirm='deleteHandle(scope.row)' class="popconfirm">
                        <el-button type='text' slot="reference">删除</el-button>
                    </el-popconfirm>
                    <el-button type='text' @click='createHandle(scope.row)'>添加字典值</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
    import {toast, alert, createField} from '@u'
    export default {
        data () {
            return {
                tableData : [],
                queryForm : {
                    parent_id : 0,
                    page : 1,
                    limit : 100
                },
                total : 0,
                history : [0],//查看下级菜单的历史
                isLoading : false,
                createForm : {
                    title : null,
                    value : null,
                    parent_id : 0
                }
            }
        },
        created () {
            this.getTableData();
        },
        methods : {
            getTableData () {
                this.isLoading = true;
                this.__api.system.dictionaryList({
                    ...this.queryForm,
                    parent_id : this.history[this.history.length - 1]
                }).then(res => {
                    this.isLoading = false;
                    this.total = res.total;
                    this.tableData = res.total > 0 ? createField(res.data, {children : []}) : res.data;
                })
            },
            //删除
            deleteHandle (row, flag) {
                this.__api.system.dictionaryDelete({
                    id : row.id
                }).then(_ => {
                    toast('操作成功');
                    this.getTableData();
                })
            },
            //更新
            updateHandle (row) {
                this.$nextTick(_ => {
                    this.__api.system.dictionaryUpdate(row).then(_ => {
                        toast('操作成功')
                    })
                })
            },
            //查看下级菜单
            viewChildren (row) {
                this.history = [...this.history, row.id];
                this.queryForm.page = 1;
                this.getTableData();
            },
            //返回上一级列表
            backToParent () {
                this.history.pop();
                this.queryForm.page = 1;
                this.getTableData();
            },
            //添加下级菜单
            createHandle ({id}) {
                this.createForm.parent_id = id || 0;
                this.$refs.popover.doShow();
            },
            //创建确认
            createConfirm () {
                if (!this.createForm.title) {
                    toast('请填写字典名称', 'error');
                    return;
                }
                if (!this.createForm.value) {
                    toast('请填写字典code', 'error');
                    return;
                }
                this.__api.system.dictionaryCreate(this.createForm).then(_ => {
                    toast('操作成功');
                    this.$refs.popover.doClose();
                    (this.createForm.parent_id == 0) && this.getTableData();
                    this.resetPopover();
                })
            },
            //重置相关数据
            resetPopover () {
                this.createForm = {
                    title : null,
                    value : null,
                    parent_id : 0
                }
            },
            expandChange (row) {
                this.__api.system.dictionaryList({
                    parent_id : row.id,
                    page : 1,
                    limit : 99999999
                }).then(res => {
                    row.children = res.data;
                    this.$forceUpdate();
                })
            }
        }
    }
</script>

<style>

</style>